if '__main__' == __name__:

    def main():
        data = [
            [1, 2],
            [8, 9],
            [8, 7],
            [10, -1],
            [5, 6],
            [3, 4],
            [5, 4],
        ]

        sets = []
        xlen = None
        singles = set()
        no = 0

        def is_goon():
            nonlocal xlen
            xlen_new = sum([len(xset) for xset in sets])
            if xlen is None:
                xlen = xlen_new
                return True
            elif xlen == xlen_new:
                return False
            else:
                xlen = xlen_new
                return True

        while True:
            no += 1
            print(no)
            for pair in data:
                a, b = pair
                if not len(sets):
                    sets.append(set([a, b]))
                if -1 == a and -1 == b:
                    continue
                if -1 == a:
                    if not a in singles:
                        sets.append(set([b]))
                        singles.add(a)
                    continue
                if -1 == b:
                    if not b in singles:
                        sets.append(set([a]))
                        singles.add(b)
                    continue
                found = False
                for xset in sets:
                    if a in xset:
                        found = True
                        xset.add(b)
                    if b in xset:
                        found = True
                        xset.add(a)
                if not found:
                    sets.append(set([a, b]))
            if not is_goon():
                break

        dist = set()
        for xset in sets:
            tup = tuple(xset)
            dist.add(tup)

        for tup in dist:
            print(sorted(tup))

    main()
